﻿using ProjectManager.Interface;
using ProjectManager.Logic;
using ProjectManager.Logic.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManager.Controller
{
    internal partial class LogicController : ILogic
    {
        public List<TeamInformation> GetTeams()
        {
            if (this.currentProject == null) return [];
            return sqlController.GetTeams(CurrentProject).TeamSort(CurrentIdentity);
        }
        public void CreateTeam(string teamName, bool IsIndivual = false)
        {
            var teams = sqlController.GetTeams(CurrentProject);
            var teamID = teams.GenerateTeamID();
            TeamInformation teamInformation = new()
            {
                ID = teamID,
                Name = teamName,
                Leader = CurrentIdentity,
                Members = [],
                Tasks = [],
                IsIndividual = IsIndivual,
                IsCancelled = false
            };
            sqlController.AddTeam(CurrentProject, teamInformation);
            CommitAndPush(CurrentProject.MemberPath, "Member", "Create Team", teamName);
        }
        public void TeamAddMember(TeamInformation rootTeam, TeamInformation addedMember)
        {
            rootTeam.Members.Add(addedMember);
            sqlController.UpdateTeam(CurrentProject, rootTeam);
            CommitAndPush(CurrentProject.MemberPath, "Member", "Team Add Member", addedMember.Name);
        }

        public void TeamRemoveMember(TeamInformation rootTeam, TeamInformation removedMember)
        {
            rootTeam.Members.Remove(removedMember);
            sqlController.UpdateTeam(CurrentProject, rootTeam);
            CommitAndPush(CurrentProject.MemberPath, "Member", "Team Remove Member", removedMember.Name);
        }
        public void DisbandTeam(TeamInformation rootTeam)
        {
            if (rootTeam.Leader.Equals(CurrentIdentity))
            {
                rootTeam.IsCancelled = true;
                sqlController.UpdateTeam(CurrentProject, rootTeam);
                CommitAndPush(CurrentProject.MemberPath, "Member", "Disband Team", rootTeam.Name);
            }
            else
            {
                ShowErrorMessage?.Invoke("没有权限");
            }
        }
    }
}
